﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.8/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
    <div class="layuimini-container">
        <div class="layuimini-main">

            <!--<fieldset class="table-search-fieldset">
                <legend>搜索信息</legend>
                <div style="margin: 10px 10px 10px 10px">
                    <form class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">角色名</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="RoleName" autocomplete="off" class="layui-input">
                                </div>
                                <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset>-->

            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-group">
                    <button type="button" class="layui-btn" lay-event="add">增加</button>
                    <button type="button" class="layui-btn " lay-event="delete">删除</button>
                </div>
                <div class="layui-btn-group">
                    <button type="button" class="layui-btn " lay-event="Nullity:0">启用</button>
                    <button type="button" class="layui-btn" lay-event="Nullity:1">禁用</button>
                    <button type="button" class="layui-btn" lay-event="IsIdent"> 解绑两步认证 </button>
                </div>

                <div class="layui-btn-group">
                    <button type="button" class="layui-btn" lay-event="axzt">踢 出</button>
                </div>

                <div class="layui-btn-group">
                    <button type="button" class="layui-btn layui-btn-primary" lay-event="data-search-btn">刷 新</button>
                </div>
            </script>

            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

            <script type="text/html" id="currentTableBar">
                <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            </script>

            <script type="text/html" id="Nullity">
                {{#  if(d.Nullity === 0){ }}
                <span style="color: #25b99e;">启用</span>
                {{#  } else { }}
                <span style="color: #F581B1;">禁用</span>
                {{#  } }}
            </script>
            <script type="text/html" id="IsIdent">
                {{#  if(d.IsIdent === 0){ }}
                <span style="color: #25b99e;">未绑定</span>
                {{#  } else { }}
                <span style="color: #F581B1;">已绑定</span>
                {{#  } }}
            </script>
        </div>
    </div>
    <script src="../../lib/layui-v2.6.8/layui.js" charset="utf-8"></script>
    <script src="../../js/lay-config.js?v=1.1.0" charset="utf-8"></script>
    <script>
        layui.use(['form', 'table', 'nixue'], function () {
            if (!layui.AddRoute()) return;
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table,
                nx = layui.nixue;

            table.render({
                elem: '#currentTableId',
                url: '../../Api/AdminServers/GetBaseUserLists',
                method: "post",
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter', 'exports', 'print'],
                cols: [[
                    { type: "checkbox", align: "center" },
                    { field: 'BaseName', align: "center", title: '用户名' },
                    { field: 'BaseRank', align: "center", title: '角色' },
                    { field: 'AddTime', align: "center", title: '添加时间', sort: true, templet: function (res) { return '<em>' + nx.GetDateString('yyyy-MM-dd hh:mm:ss', res.AddTime) + '</em>' } },
                    { field: 'Nullity', align: "center", title: '状态', sort: true, templet: '#Nullity' },
                    { field: 'Logins', align: "center", title: '登录次数' },
                    { field: 'IsIdent', align: "center", title: '两步认证', sort: true, templet: '#IsIdent' },
                    { field: 'LastLoginIP', align: "center", title: '上次登陆地址' },
                    { field: 'LastLoginDate', align: "center", title: '上次登陆时间', sort: true, templet: function (res) { return '<em>' + nx.GetDateString('yyyy-MM-dd hh:mm:ss', res.LastLoginDate) + '</em>' } },
                    { field: 'FinalLoginIP', align: "center", title: '最后登陆地址' },
                    { field: 'FinalLoginDate', align: "center", title: '最后登陆时间', sort: true, templet: function (res) { return '<em>' + nx.GetDateString('yyyy-MM-dd hh:mm:ss', res.FinalLoginDate) + '</em>' } },
                    //{ field: 'axzt', align: "center", width: 80, title: '在线状态', toolbar: '#axzt' },
                    { title: '修改', minWidth: 150, toolbar: '#currentTableBar', align: "center" }
                ]],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 15,
                page: true,
                done: function (res, curr, count) {
                    nx.TableDone({
                        table: this.elem,
                        data: res.data,
                        //key: "BaseIp",
                        on: function () {
                            var that = this;
                            nx.GetIpAddress(that.data.FinalLoginIP).then(function (a) { //console.log(a);
                                that.tds.FinalLoginIP.text(a);//that.td.text(a);
                            });
                            nx.GetIpAddress(that.data.LastLoginIP).then(function (a) { //console.log(a);
                                that.tds.LastLoginIP.text(a);//that.td.text(a);
                            });

                            //if (res.admin === that.data.BaseName) {
                            //    that.tds.axzt.html(`<span style="color: red">当前在线</span>`);
                            //} else {
                            //    var list = res.NameKey;
                            //    var html1 = `<a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="Xx">在线</a>`;
                            //    var html2 = `<span style="color: #999999b5">离线</span>`;
                            //    for (var i = 0; i < list.length; i++) {
                            //        if (that.data.BaseName == list[i]) {
                            //            that.tds.axzt.html(html1);
                            //            break;
                            //        } else {
                            //            that.tds.axzt.html(html2);
                            //        }
                            //    }
                            //}
                        }
                    });
                    nx.TableSort(this, table);
                },
                skin: 'line',
                height: 'full-60',
                cellMinWidth: 80,
                parseData: function (res) {
                    var data = nx.TableParseData(res);
                    data.NameKey = res.data.NameKey;
                    data.admin = res.data.admin;
                    return data;
                },
                autoSort: false
            });

            function tablereload(data) {
                //执行搜索重载
                table.reload('currentTableId', {
                    page: {
                        curr: 1
                    },
                    //, where: result,
                    where: data
                }, 'data');
            }

            // 监听搜索操作
            nx.OnForm('submit(data-search-btn)', function (data) {
                //var result = JSON.stringify(data.field);
                //layer.alert(result, {
                //    title: '最终的搜索信息'
                //});
                tablereload(data);
                return false;
            });

            /**
             * toolbar监听事件
             */
            table.on('toolbar(currentTableFilter)', function (obj) {
                if (obj.event === 'add') {  // 监听添加操作
                    var index = layer.open({
                        title: '添加用户',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['50%', '50%'],
                        content: '/Admin/ManageEdit?id=0',
                        end: function () {
                            // 右上角关闭事件的逻辑
                            //layui.event('form', 'submit(data-search-btn)');
                            tablereload();
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                }
                else if (obj.event === 'delete') {  // 监听删除操作
                    var checkStatus = table.checkStatus('currentTableId')
                        , data = checkStatus.data;
                    if (data.length === 0) {
                        layer.msg('请选中需要操作的数据！', { offset: '6px' });
                        return;
                    } else {
                        var getidlist = nx.GetIds(data, 'ID');
                        var Namelist = nx.GetIds(data, 'BaseName');
                        nx.Psot(function (result) {
                            if (result.code==0) {
                                Logout(Namelist);
                                tablereload();
                                nx.msg(result.msg, null, 1);
                            } else {
                                nx.msg(result.msg, null, 2);
                            }
                          
                        }, '../../Api/AdminServers/DeleteBaseUsers', { idlist: getidlist });
                    }
                }
                else if (obj.event.startsWith('Nullity')) {  // 监听启用/禁用 操作

                    var nullity = obj.event.endsWith('0') ? 0 : 1;
                    var checkStatus = table.checkStatus('currentTableId')
                        , data = checkStatus.data;
                    if (data.length === 0) {
                        layer.msg('请选中需要操作的数据！', { offset: '6px' });
                        return;
                    } else {
                        var idlist = nx.GetIds(data, 'ID');
                        nx.Psot(function (result) {
                            if (result.code == 0) {
                                tablereload();
                                layer.msg(result.msg);
                            } else {
                                layer.msg(result.msg);
                            }
                        }, '../../Api/AdminServers/SetNullitys', { idlist: idlist, Nullity: nullity });
                    }
                } else if (obj.event === 'IsIdent') {  // 监听解绑操作
                    var checkStatus = table.checkStatus('currentTableId')
                        , data = checkStatus.data;
                    if (data.length === 0) {
                        layer.msg('请选中需要操作的数据！', { offset: '6px' });
                        return;
                    } else {
                        var getidlist = nx.GetIds(data, 'ID');
                        nx.Psot(function (result) {
                            tablereload();
                            layer.msg(result.msg);
                        }, '../../Api/AdminServers/SetBaseIsIdents', { idlist: getidlist, isIdent: 0 });
                    }
                } else if (obj.event === 'data-search-btn') {
                    tablereload(data);
                    return false;
                } else if (obj.event === 'axzt') {
                    var checkStatus = table.checkStatus('currentTableId')
                        , data = checkStatus.data;
                    if (data.length === 0) {
                        layer.msg('请选中需要操作的数据！', { offset: '6px' });
                        return;
                    }else if (data.length > 1) {
                        layer.msg('请选中单个账号！', { offset: '6px' });
                        return;
                    }
                    else {
                        var getidlist = nx.GetIds(data, 'BaseName');
                        nx.confirm('确定将该管理员下线吗？', function (index) {
                            nx.Psot(function (result) {
                                tablereload();
                                nx.alert(result.msg);
                            }, '../../Api/AdminServers/ManageLogouts', { UserName: getidlist });
                        });
                    }
                    return false;
                }
            });

            //监听表格复选框选择
            table.on('checkbox(currentTableFilter)', function (obj) {
                console.log(obj)
            });



            function Logout(BaseName) {
                nx.Psot(function (result) {
                }, '../../Api/AdminServers/ManageLogouts', { UserName: BaseName });
            }


            table.on('tool(currentTableFilter)', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') {
                    var index = layer.open({
                        title: '编辑管理员信息',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['50%', '50%'],
                        content: '/Admin/ManageEdit?id=' + data.ID,
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                } else if (obj.event === 'Xx') {
                    nx.confirm('确定将该管理员下线吗？', function (index) {
                        nx.Psot(function (result) {
                            tablereload();
                            nx.alert(result.msg);
                        }, '../../Api/AdminServers/ManageLogouts', { UserName: data.BaseName });
                    });

                }
            });

        });
    </script>

</body>
</html>